tidyTuesday: Spending on Kids

Author

Robert W. Walker

Spending on Kids

First, let me import the data.

Code
kids <- read.csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')
# kids <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')

Now let me summarise it and show a table of the variables.

Code
summary(kids)
    state             variable              year           raw          
 Length:23460       Length:23460       Min.   :1997   Min.   :  -60139  
 Class :character   Class :character   1st Qu.:2002   1st Qu.:   71985  
 Mode  :character   Mode  :character   Median :2006   Median :  252002  
                                       Mean   :2006   Mean   : 1181359  
                                       3rd Qu.:2011   3rd Qu.:  836324  
                                       Max.   :2016   Max.   :83666088  
                                                      NA's   :102       
    inf_adj         inf_adj_perchild  
 Min.   :  -60799   Min.   :-0.01361  
 1st Qu.:   85876   1st Qu.: 0.12456  
 Median :  298778   Median : 0.32757  
 Mean   : 1359983   Mean   : 0.91448  
 3rd Qu.:  985049   3rd Qu.: 0.83362  
 Max.   :84584960   Max.   :20.27326  
 NA's   :102        NA's   :102       

A table of the variables. The definitions are best found here.

Code
table(kids$variable)

        addCC           CTC       edservs        edsubs       fedEITC 
         1020          1020          1020          1020          1020 
       fedSSI           HCD HeadStartPriv      highered           lib 
         1020          1020          1020          1020          1020 
Medicaid_CHIP  other_health othercashserv       parkrec          pell 
         1020          1020          1020          1020          1020 
       PK12ed     pubhealth          SNAP        socsec     stateEITC 
         1020          1020          1020          1020          1020 
    TANFbasic         unemp         wcomp 
         1020          1020          1020 

It is very tidy. It is probably better shown after a pivot. 50 states, the District of Columbia, and 20 years gives us 1,020 observations. Let me show it wide.

Code
library(DT)
Big.Wide <- pivot_wider(kids, id_cols = c(state,year), names_from = "variable", values_from = c("raw","inf_adj","inf_adj_perchild"))
datatable(Big.Wide)

My brief plan

I recently came across a geofacet for R. I want to use it to plot a little bit of this data. If you want to get a head start, try install.packages("geofacet", dependencies=TRUE). You can google geofacet to get an idea of what a geofacet plot is. I will build one on the fly using a couple of tidy tools: filter, mutate, and joins and then put it together.

Code
library(geofacet)

From the vignette, we have a working example.

Code
ggplot(state_ranks, aes(variable, rank, fill = variable)) +
  geom_col() +
  coord_flip() +
  theme_bw() +
  facet_geo(~ state)

Code
State.Temp <- state_ranks %>% filter(variable=="education")
Final.Data <- left_join(kids, State.Temp, by = c('state' = 'name')) %>% filter(variable.x == "PK12ed")
ggplot(Final.Data, aes(x=year, y=inf_adj_perchild, color = inf_adj_perchild)) +
  geom_line() +
  theme_void() +
  scale_color_viridis_c() +
  facet_geo(~ state.y) + labs(title="Pre-K through 12 Education Spending per Child", subtitle="Inflation adjusted", color="Spend per Child") -> My.Plot
My.Plot

Getting Real Fancy

gganimate makes animation an easy addition to basic ggplots. The same developer is also responsible for the very accessible patchwork for combining multiple ggplots. I will use the ability to reveal a transition in this animation.

Code
library(gganimate)
My.Anim <- My.Plot + transition_reveal(year)
anim_save("KidsAnim.gif")

Animation

References

Code
knitr::write_bib(names(sessionInfo()$otherPkgs), file="bibliography.bib")

References

Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Hafen, Ryan. 2020. Geofacet: Ggplot2 Faceting Utilities for Geographical Data. https://github.com/hafen/geofacet.
Müller, Kirill, and Hadley Wickham. 2023. Tibble: Simple Data Frames. https://tibble.tidyverse.org/.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2023. Lubridate: Make Dealing with Dates a Little Easier. https://lubridate.tidyverse.org.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2023a. Forcats: Tools for Working with Categorical Variables (Factors). https://forcats.tidyverse.org/.
———. 2023b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://stringr.tidyverse.org.
———. 2023c. Tidyverse: Easily Install and Load the Tidyverse. https://tidyverse.tidyverse.org.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2023. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://ggplot2.tidyverse.org.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.
Wickham, Hadley, and Lionel Henry. 2023. Purrr: Functional Programming Tools. https://purrr.tidyverse.org/.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2023. Readr: Read Rectangular Text Data. https://readr.tidyverse.org.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2023. Tidyr: Tidy Messy Data. https://tidyr.tidyverse.org.
Xie, Yihui, Joe Cheng, and Xianying Tan. 2023. DT: A Wrapper of the JavaScript Library DataTables. https://github.com/rstudio/DT.